export default {
  props: {

  },
  data () {
    return {
      changeAttributeTitleTemp: '', // 暂存名称
      changeAttributeTitleTempArr: '', // 暂存名称Arr
      specNameTemp: { // 添加规格时使用的临时存放
        name: '',
        item: ''
      },
      inputVisible: false,
      inputValue: '',
      dictionaries: { // 字典数据
        sale_status: [], // 状态
        goods_type: [] // 商品类型
      },
      parArray: [] // 按钮权限
    }
  },
  computed: {
    // 可选择规格数量
    multipleLimitQuantity () {
      const selectLength = this.goodsSaleDataListValue.length
      const attributeLength = this.skuFormConfig.attribute.length
      const tempLength = this.skuFormConfig.inputValueArrTemp.length
      const quantity = this.goodsSaleDataListValue.length + this.skuFormConfig.attribute.length
      console.log('selectLength', typeof selectLength, selectLength)
      console.log('attributeLength', typeof attributeLength, attributeLength)
      console.log('tempLength', typeof tempLength, tempLength)
      console.log('quantity', typeof quantity, quantity)
      console.log(`=========================================`)
      if (quantity >= 3) {
        return this.skuFormConfig.attribute.length
      } else {
        return quantity
      }
    }
  },
  created () {
    // 获取字典值
    for (const type in this.dictionaries) { this.getDictionaries(type) }
  },
  methods: {
    // 获取字典数据
    getDictionaries (type) {
      if (type && this.dictionaries[type].length > 0) { return }
      const params = { groupId: type }
      this.$api.dictionaries(params).then((res) => {
        this.dictionaries[type] = res.data
      }).catch((err) => {
        console.log(err)
      })
    },
    getButtonTreeDto () {
      // 获取权限设定
      this.limits = JSON.parse(sessionStorage.getItem('limit'))
      const params = this.limits.buttonTreeDto.filter((item) => item.menuCode === 'companyCode')
      params[0].buttonList.forEach((item) => { this.parArray.push(item.code) })
      // console.log(this.parArray)
    },
    handleClose (tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    onFocus (index) {
      // console.log(index)
      if (this.currentPageStatus === 'add') {
        // this.skuFormConfig.inputValueArr.push('')
        // this.skuFormConfig.inputValueArrTemp.push('')
      }
    },
    showInput () {
      this.inputVisible = true
      console.log('showInput', this.skuFormConfig)
      this.$nextTick(() => {
        // this.$refs.saveTagInput.$refs.input.focus();
      })
    },
    // 添加规格大类
    addSpecName () {
      const objTemp = this.specNameTemp
      // const obj = { item: [objTemp.item], itemList: [{ content: objTemp.item }], name: objTemp.name }
      let skuNo = this.maxSkuNo
      skuNo = skuNo < 10 ? '0' + skuNo.toString() : skuNo.toString()
      const obj = { item: [], itemList: [], name: objTemp.name, specNo: skuNo }
      obj.item.push(objTemp.item)
      obj.itemList.push({ content: objTemp.item, sort: 1 })
      if (objTemp.name === '' || objTemp.item === '') {
        this.$message.warning('填写规格内容不能为空！')
        console.log('填写规格内容不能为空！')
      } else {
        this.skuFormConfig.sourceAttribute.push(obj)
        this.skuFormConfig.attribute.push(obj)
        this.skuFormConfig.inputValueArr = []
        this.skuFormConfig.inputValueArrTemp = []
        this.skuFormConfig.sourceAttribute.forEach((item, index) => {
          this.skuFormConfig.inputValueArr.push([])
          this.skuFormConfig.inputValueArrTemp.push('')
        })

        this.specNameTemp = { name: '', item: '' }
        this.inputVisible = false
        this.maxSkuNo++
        this.$nextTick(_ => {
          this.$refs.skuForm.init()
        })
        console.log('addSpecName', this.skuFormConfig)
      }
    },
    // 获得焦点时触发 时 缓存名称
    changeAttributeTitleFocus (name, fatherIndex) {
      // console.log(this.skuFormConfig.attribute[fatherIndex].name)
      this.changeAttributeTitleTemp = name
      const sourceAttribute = this.skuFormConfig.sourceAttribute
      const sourceAttributeNameArr = []
      sourceAttribute.forEach(item => {
        sourceAttributeNameArr.push(item.name)
      })
      this.changeAttributeTitleTempArr = sourceAttributeNameArr
      console.log(this.changeAttributeTitleTempArr)
    },
    changeAttributeTitle (name, fatherIndex) {
      if (name) {
        if (this.changeAttributeTitleTemp !== name) {
          const changeAttributeTitleTempArr = this.changeAttributeTitleTempArr
          // 检测是否存在重名
          let isRepeat = false
          for (let index = 0; index < changeAttributeTitleTempArr.length; index++) {
            if (changeAttributeTitleTempArr[index] === name) {
              isRepeat = true
              break
            }
          }
          if (isRepeat) {
            this.skuFormConfig.attribute[fatherIndex].name = this.changeAttributeTitleTemp
            this.$message.warning('存在有相重的规格名称')
            console.log('存在有相重的规格名称', isRepeat)
          } else {
            this.$message.success('修改规格名成功！')
            this.$refs.skuForm.init()
            console.log('修改成功！')
          }
        } else {
          console.log('不需要修改！')
        }
      } else {
        this.skuFormConfig.attribute[fatherIndex].name = this.changeAttributeTitleTemp
        this.$message.warning('填写的规格名不能为空！')
        console.log('填写的名称不能为空！')
      }
    },
    onChangeContent (fatherIndex, childrenIndex) {
      // debugger
      // 修改前的文本
      const beforeText = this.skuFormConfig.attribute[fatherIndex].item[childrenIndex]
      // 修改后的文本
      const changeText = this.skuFormConfig.attribute[fatherIndex].itemList[childrenIndex].content
      if (changeText === '') {
        this.$message.warning('填写内容不能为空！')
        console.log('填写内容不能为空！')
        this.skuFormConfig.attribute[fatherIndex].itemList[childrenIndex].content = beforeText
      } else {
        this.skuFormConfig.attribute[fatherIndex].item[childrenIndex] = changeText
        this.skuFormConfig.attribute[fatherIndex].itemList[childrenIndex].content = changeText
        this.skuFormConfig.sku.forEach(item => {
          item.sku = item.sku.replace(beforeText, changeText)
        })
        this.$refs.skuForm.init()
        console.log(this.skuFormConfig)
        // sessionStorage.setItem('skuFormConfigSku', JSON.stringify(goodsSpecTable))
      }
    },
    // 删除规格值
    onDeleteContent (fatherIndex, childrenIndex, type) {
      this.$confirm('删除规格值, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (type === 'father') {
          const fatherId = this.skuFormConfig.attribute[fatherIndex].id
          const fatherIdIndex = this.goodsSaleDataListValue.findIndex((item) => { return item === fatherId })
          this.goodsSaleDataListValue.splice(fatherIdIndex, 1)
          this.skuFormConfig.attribute.splice(fatherIndex, 1)
        } else {
          this.skuFormConfig.attribute[fatherIndex].itemList.splice(childrenIndex, 1)
          this.skuFormConfig.attribute[fatherIndex].item.splice(childrenIndex, 1)
        }
        this.$nextTick(_ => {
          this.$refs.skuForm.init()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleInputConfirm (index) {
      // const inputValue = this.skuFormConfig.inputValueArr[index]
      const inputValue = this.skuFormConfig.inputValueArrTemp[index]
      if (inputValue) {
        const obj = { content: inputValue, sort: 1 }
        this.skuFormConfig.inputValueArr[index].push(obj)
        this.skuFormConfig.attribute[index].itemList.push(obj)
        this.skuFormConfig.attribute[index].item.push(inputValue)
        this.$refs.skuForm.init()
        this.skuFormConfig.inputValueArrTemp[index] = ''
      }
      // console.log(this.skuFormConfig)
    },
    changeInput () {
      // console.log('changeInput')
      this.$forceUpdate()
    },
    // 获取上传图片文件的链接
    getUrl (getUrl) {
      // if(e.mainImage && e.mainImage.indexOf("http://fenxiao.muruji.com/") !== 0){e.mainImage = this.ImgHttps + e.mainImage}
      this.infoForm.imageList.push({ mainFlag: 0, url: this.ImgHttps + getUrl })
      // console.log(this.infoForm.imageList)
    },
    // 删除当前图片文件的链接（如果接受文件路径是数组时 使用此方法）
    handleRemove (url) {
      const delIndex = this.infoForm.imageList.findIndex(e => { return e.url == url })
      this.infoForm.imageList.splice(delIndex, 1)
    },
    // 重置表单
    resetForm (formName) { this.$refs[formName].resetFields() },
    // 页面跳转
    toOtherPages (name, parameter, type) {
      let query = { id: parameter, type }
      if (type === 'category') { query = { category: parameter, type } }
      this.$router.push({ name, query })
    }
  }
}

